package com.ivncorp.evnproject.adapter;

import java.util.ArrayList;

import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.ivncorp.evnproject.ActivityGalleryUrl;
import com.ivncorp.evnproject.ActivityMapOnline;
import com.ivncorp.evnproject.R;
import com.ivncorp.evnproject.api.JSONParser;
import com.ivncorp.evnproject.api.RestAPI;
import com.ivncorp.evnproject.model.UploadTable;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

public class TimelineAdapter extends BaseAdapter {

	private Activity activity;
	private ArrayList<UploadTable> data;
	private static LayoutInflater inflater = null;
	private ImageLoader imageLoader;
	private DisplayImageOptions options;
	

	public TimelineAdapter(Activity a, ArrayList<UploadTable> d) {
		activity = a;
		data = d;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = ImageLoader.getInstance();

		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_action_camera)
				.showImageForEmptyUri(R.drawable.ic_action_group)
				.showImageOnFail(R.drawable.ic_action_person)
				.cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
				.displayer(new RoundedBitmapDisplayer(0)).build();
	}

	public int getCount() {
		return data.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	static class ViewHolder {
		TextView tvname;
		ImageView imageView;
		TextView tvcode;
		TextView tvcapacity;
		LinearLayout ibtn_location;
		ProgressBar progressBar;
		ImageButton button;
	}

	@SuppressWarnings("deprecation")
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		final ViewHolder holder;
		if (view == null) {
			view = inflater.inflate(R.layout.item_list_timeline, parent, false);
			holder = new ViewHolder();
			assert view != null;
			holder.tvname = (TextView) view.findViewById(R.id.txtNameTimeline);
			holder.tvcode = (TextView) view.findViewById(R.id.txtCode);
			holder.tvcapacity = (TextView) view.findViewById(R.id.txtCapacity);
			holder.imageView = (ImageView) view.findViewById(R.id.imageView);
			holder.button = (ImageButton) view.findViewById(R.id.btnDelete);
			holder.ibtn_location = (LinearLayout) view
					.findViewById(R.id.btnMap);
			holder.progressBar = (ProgressBar) view.findViewById(R.id.loading);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		Display display = activity.getWindowManager().getDefaultDisplay();
		UploadTable uploadtable = new UploadTable();
		uploadtable = data.get(position);

		holder.tvname.setText(uploadtable.getName());
		holder.tvcode.setText(uploadtable.getCode());
		holder.tvcapacity.setText("" + uploadtable.getCapacity());
		holder.ibtn_location.setOnClickListener(new OnclicklistenerButton(
				position));
		holder.imageView.setOnClickListener(new OnclicklistenerImage(uploadtable));
		holder.imageView.getLayoutParams().height = display.getHeight() / 2 - 30;
		imageLoader.displayImage(uploadtable.getImage1(),
				holder.imageView, options, new SimpleImageLoadingListener() {
					@Override
					public void onLoadingStarted(String imageUri, View view) {
						holder.progressBar.setVisibility(View.VISIBLE);
					}

					@Override
					public void onLoadingFailed(String imageUri, View view,
							FailReason failReason) {
						holder.progressBar.setVisibility(View.GONE);
					}

					@Override
					public void onLoadingComplete(String imageUri, View view,
							Bitmap loadedImage) {
						holder.progressBar.setVisibility(View.GONE);
					}
				});

		// popup
		holder.button.setOnClickListener(new OnclicklistenerButtonDelete(uploadtable.getId()));
		return view;
	}
	
	class OnclicklistenerButtonDelete implements OnClickListener {

		int _id;

		public OnclicklistenerButtonDelete(int id) {
			this._id = id;
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			AlertDialog.Builder builder = new AlertDialog.Builder(activity);
			builder.setMessage(R.string.delete_2)
					.setTitle(R.string.delete_title)
					.setPositiveButton("Ok",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									RestAPI api = new RestAPI();

									try {
//										JSONObject ob=api.DeleteUploadTable(_id);
//										JSONParser part=new JSONParser();
//										if(part.parseSuccess(ob))
//										{
//											Toast.makeText(activity, "Thanh Cong", Toast.LENGTH_SHORT);
//										}
//										else
//										{
//											Toast.makeText(activity, "That Bai", Toast.LENGTH_SHORT);
//										}
									} catch (Exception e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}

								}
								
							})
					.setNegativeButton("Cancel",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									// Handle Cancel

								}
							}).show();

		}

	}
	

	class OnclicklistenerImage implements OnClickListener {

		UploadTable table;

		public OnclicklistenerImage(UploadTable table) {
			this.table = table;
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(activity, ActivityGalleryUrl.class);
			intent.putExtra("object", table);
			activity.startActivity(intent);
			activity.overridePendingTransition(R.anim.right_in, R.anim.left_out);
		}

	}

	class OnclicklistenerButton implements OnClickListener {
		int _position;

		public OnclicklistenerButton(int position) {
			this._position = position;
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(activity, ActivityMapOnline.class);
			intent.putExtra("input", "object");
			intent.putExtra("uploadtable", data.get(_position));
			activity.startActivity(intent);
			activity.overridePendingTransition(R.anim.right_in, R.anim.left_out);
		}

	}
}